/****************************************************************************
  @file    : AR-RAF1C.c
  @Version : V1.0
  @Company : Nanjing Puietel Internet of Things technology Co., Ltd.
  @author  : lixl
  @date    : 2018-11-08
  @brief   : FUJITSU空调遥控器编码
*****************************************************************************/

/* Includes ----------------------------------------------------------------*/
#include "aircon.h"
#include "string.h"

/* Private typedef ---------------------------------------------------------*/
/* Private define ----------------------------------------------------------*/
//时间定义
#define  FUJITSU_LEADCODE1_TIME_H      (uint16_t)( 0x8000   | (uint16_t)3300 )//低电平3.33ms
#define  FUJITSU_LEADCODE1_TIME_L     (uint16_t)( (~0x8000)& (uint16_t)1570 )//高电平1.57ms

#define  FUJITSU_ENDCODE_TIME_H       (uint16_t)( 0x8000   | (uint16_t)450  )
#define  FUJITSU_ENDCODE_TIME_L       (uint16_t)( (~0x8000)& (uint16_t)5300)
#define  FUJITSU_BIT0_TIME_H        (uint16_t)( 0x8000   | (uint16_t)450  )
#define  FUJITSU_BIT0_TIME_L        (uint16_t)( (~0x8000)& (uint16_t)380  )
#define  FUJITSU_BIT1_TIME_H        (uint16_t)( 0x8000   | (uint16_t)450  )
#define  FUJITSU_BIT1_TIME_L        (uint16_t)( (~0x8000)& (uint16_t)1210 )

/* Private macro -----------------------------------------------------------*/
/* Private variables -------------------------------------------------------*/
static uint16_t FUJITSUSendlen = 0;               //发送帧组包长度

const uint8_t  FUJITSU_OFF_MODE_ARRAY[7] = {0x14,0x63,0x00,0x10,0x10,0x02,0xFD};
const uint8_t  FUJITSU_ON_MODE_ARRAY[16] = {0x14,0x63,0x00,0x10,0x10,0xFE,0x09,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x00};

/* Private function prototypes ---------------------------------------------*/
static void AR_RAF1CPackageByte(uint8_t byte);
uint8_t AirConOnOff_Pre = 2;//关机状态
/* Private functions -------------------------------------------------------*/
static void AR_RAF1CPackageByte(uint8_t byte) {
  uint8_t i;

  for(i = 0; i<8; i++) {
    if(byte&(0x01<<(i))) {//if(byte&(0x80>>(i))) {  //高电平
      IrSendBuf[FUJITSUSendlen++] = FUJITSU_BIT1_TIME_H;
      IrSendBuf[FUJITSUSendlen++] = FUJITSU_BIT1_TIME_L;
    } else {        //低电平
      IrSendBuf[FUJITSUSendlen++] = FUJITSU_BIT0_TIME_H;
      IrSendBuf[FUJITSUSendlen++] = FUJITSU_BIT0_TIME_L;
    }
  }
}

/* Public functions --------------------------------------------------------*/
void AR_RAF1CIrPackage(void *p) {
  uint8_t i,tmp = 0;
  uint8_t data_offset;
  

  uint8_t byte[16];

  uint8_t AirConOnOff = ((uint8_t*)p)[0];
  uint8_t AirConMode = ((uint8_t*)p)[1];
  uint8_t AirConFan = ((uint8_t*)p)[2];
  uint8_t AirConTmp = ((uint8_t*)p)[3];

  //设定温度
  if(AirConTmp > 30)  AirConTmp = 30;
  else if(AirConTmp < 18)  AirConTmp = 18;

  tmp = AirConTmp - 18;

  //开关码
  if(AirConOnOff == AIRCON_ON) {      //开空调
      for(i = 0; i < 16; i ++) {
        byte[i] = FUJITSU_ON_MODE_ARRAY[i];
      }
    if(AirConMode == AIRCON_COOLING) {      //制冷
      data_offset = 0;
      byte[9] |= 0x01;
//      byte[15] |= 0x0f;
    } else if(AirConMode == AIRCON_HEATING) { //制热
      data_offset = 3;
      byte[9] |= 0x04;
//      byte[15] |= 0x0c;
    } else if(AirConMode == AIRCON_WENTING){  //通风除湿
      data_offset = 1;
      byte[9] |= 0x02;
//      byte[15] |= 0x0e;
    }
    else{
      return;
    }   
          //风速
      if(AirConFan == AIRCON_WINDLOW)     //低速
      {
        byte[10] |= 0x03;
        byte[15] |= (0x0C-data_offset);       
      }
      else if(AirConFan == AIRCON_WINDMID)  //中速
      {
        byte[10] |= 0x02;
        byte[15] |= (0x0D-data_offset);
      }
      else if(AirConFan == AIRCON_WINDHIG)  //高速
      {
        byte[10] |= 0x01;
        byte[15] |= (0x0E - data_offset);
      }
      else if(AirConFan == AIRCON_WINDAUTO) //自动
      {
        byte[10] |= 0x00;
        byte[15] |= (0x0F-data_offset);
      }
      
    if(AirConOnOff != AirConOnOff_Pre){
      AirConOnOff_Pre = AirConOnOff;
      byte[8] |= 0x01;
    }
    
    byte[8]  |= (0x20 + (tmp<<4)&0xf0);
    byte[15] |= ((0x80 - (tmp<<4)&0xf0) < 0)?(0x80 - (tmp<<4)&0xf0 + 0x0f):(0x80 - (tmp<<4)&0xf0);
    
  } else if(AirConOnOff == AIRCON_OFF) {  //关空调
    for(i = 0; i < 7; i ++) {
      byte[i] = FUJITSU_OFF_MODE_ARRAY[i];
    }
    AirConOnOff_Pre = 2;
  }

  //while(IrDev.State != IRREMOTE_IDLE);  //等待发送完成

  memset(IrSendBuf, 0, sizeof(IrSendBuf)/sizeof(uint16_t));

  FUJITSUSendlen = 0;

  //配置引导码
  IrSendBuf[FUJITSUSendlen++] = FUJITSU_LEADCODE1_TIME_H;
  IrSendBuf[FUJITSUSendlen++] = FUJITSU_LEADCODE1_TIME_L;

  if(AirConOnOff == AIRCON_ON) {      //开空调
    for(i = 0; i < 16; i ++) {
      AR_RAF1CPackageByte(byte[i]);
    }
  }
  else if(AirConOnOff == AIRCON_OFF){
    for(i = 0; i < 7; i ++) {
      AR_RAF1CPackageByte(byte[i]);
    } 
  }
  //配置结束码
  IrSendBuf[FUJITSUSendlen++] = FUJITSU_ENDCODE_TIME_H;
  IrSendBuf[FUJITSUSendlen++] = FUJITSU_ENDCODE_TIME_L;

  //组包完成提交
  IrDev.Ptr     = IrSendBuf;
  IrDev.Len     = FUJITSUSendlen;
  IrDev.State   = IRREMOTE_REQUIRE;
}
